package com.mycontract.suitepro.contract

import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import android.view.View
import android.webkit.WebSettings
import androidx.lifecycle.ViewModel
import com.mycontract.suitepro.base.BaseActivity
import com.mycontract.suitepro.databinding.ActivityContractSuiteContractDataSafetyWebBinding
import com.mycontract.suitepro.enums.PageTagEnum
import com.mycontract.suitepro.extend.*
import com.mycontract.suitepro.utils.AppConfigUtil
import java.util.*

/** 数据声明页 */
@SuppressLint("CustomSplashScreen")
class ContractDataSafetyWebActivity : BaseActivity() {

    // 布局binding
    private lateinit var mActivityBinding: ActivityContractSuiteContractDataSafetyWebBinding

    override fun onContractSuiteBindingContractSuiteViewContractSuiteModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        return list
    }

    override fun onContractSuiteBindingContractSuiteViewContractSuiteTag(): String {
        return PageTagEnum.contract_data_safety.name.uppercase()
    }

    override fun onContractSuiteBindingContractSuiteContentContractSuiteView(): View {
        mActivityBinding = ActivityContractSuiteContractDataSafetyWebBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onContractSuiteContentContractSuiteViewContractSuiteBinded() {
        try {
            // 初始化
            initSubViews()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onResume() {
        super.onResume()
        try {
            AppConfigUtil.mRevObjAppConfig?.let { config ->
                // 获取当前的语言设置
                val locale: Locale = resources.configuration.locales[0]
                val language: String = locale.language

                if (language.startsWith("vi", true)) {
                    // 判断是否是越南语
                    config.safetyAdsVi?.let { url ->
                        if (url.isNotBlank()) {
                            println("---》fullUrl： $url")
                            mActivityBinding.appWebview.loadUrl(url)

                            // 显示loading
                            showContractSuiteLodingContractSuiteDialog()

                            Handler(Looper.getMainLooper()).postDelayed({
                                dismissContractSuiteLoadingContractSuiteDialog()
                            }, 3000)
                        }
                    }
                } else if (language.startsWith("fr", true)) {
                    config.safetyAdsFr?.let { url ->
                        if (url.isNotBlank()) {
                            println("---》fullUrl： $url")
                            mActivityBinding.appWebview.loadUrl(url)

                            // 显示loading
                            showContractSuiteLodingContractSuiteDialog()

                            Handler(Looper.getMainLooper()).postDelayed({
                                dismissContractSuiteLoadingContractSuiteDialog()
                            }, 3000)
                        }
                    }
                } else {
                    config.safetyAds?.let { url ->
                        if (url.isNotBlank()) {
                            println("---》fullUrl： $url")
                            mActivityBinding.appWebview.loadUrl(url)

                            // 显示loading
                            showContractSuiteLodingContractSuiteDialog()

                            Handler(Looper.getMainLooper()).postDelayed({
                                dismissContractSuiteLoadingContractSuiteDialog()
                            }, 3000)
                        }
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @SuppressLint("SetJavaScriptEnabled")
    private fun initSubViews() {
        with(mActivityBinding.appWebview) {
            // 清理缓存--可开可不开
            clearCache(true)
            clearHistory()
            clearFormData()
            // 设置
            settings.setSupportZoom(true)
            settings.javaScriptEnabled = true
            settings.domStorageEnabled = true
            settings.allowContentAccess = true
            settings.allowFileAccess = true
            settings.useWideViewPort = true
            settings.loadWithOverviewMode = true
            settings.builtInZoomControls = true
            settings.displayZoomControls = false
            settings.cacheMode = WebSettings.LOAD_NO_CACHE
            settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NORMAL
            settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW

            // JS支持（创建注入交互方法对象）
//            val jsInterfaceMfr = AppJavaScriptInterface(this@AppWebViewActivity)
//            addJavascriptInterface(jsInterfaceMfr, "android")
        }
    }
}